package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ItemSku struct {
	mgo.BaseDoc  `bson:"-" json:"-"`
	Id           int64      `json:"id" bson:"id,omitempty"`
	ItemBase     *ItemBase  `json:"itemBase" bson:"itemBase"`         //物品
	ItemBaseId   int64      `json:"itemBaseId" bson:"itemBaseId"`     //物品
	Text         string     `json:"text" bson:"text"`                 //物品SKU描述
	Code         string     `json:"code" bson:"code"`                 //物品SKU编码
	Unit         *Unit      `json:"unit" bson:"unit"`                 //单位
	UnitId       int64      `json:"unitId" bson:"unitId"`             //单位
	UnitBase     float64    `json:"unitBase" bson:"unitBase"`         //基本单位基数
	UnitExchange float64    `json:"unitExchange" bson:"unitExchange"` //单位转换量
	Version      int64      `json:"version" bson:"version"`           //版本
	IsOn         bool       `json:"isOn" bson:"isOn"`                 //是否使用
	CreateAt     time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt     time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt     *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *ItemSku) DocName() string { return "ItemSku" }
func (this *ItemSku) GetId() int64    { return this.Id }
func (this *ItemSku) SetId(id int64)  { this.Id = id }
func (this *ItemSku) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ItemSku) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ItemSku) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ItemSku) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ItemSku) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ItemSku) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ItemSku) ToString() string {
	return string(this.ToBytes())
}
func (this *ItemSku) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemSku) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemSku) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ItemSku) LoadItemBase() {
	if this.ItemBaseId == 0 {
		return
	}
	this.ItemBase, _ = NewItemBaseCrud().FindId(this.ItemBaseId)
}
func (this *ItemSku) LoadUnit() {
	if this.UnitId == 0 {
		return
	}
	this.Unit, _ = NewUnitCrud().FindId(this.UnitId)
}
func (this *ItemSku) ClearRelationsBeforeSave() mgo.Doc {
	this.ItemBase = nil
	this.Unit = nil
	return this
}
func neverUsed_ItemSku() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ItemSkuAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ItemSku
	Session *ez.Session
}

func NewItemSkuAccessControl(model *ItemSku, action string, session *ez.Session) *ItemSkuAccessControl {
	ctrl := &ItemSkuAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ItemSkuAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
